const router = require("koa-router")()

const request = require("request")

const crypto = require("crypto")

const wxpay = require("../util/wxPay")

var xmlreader = require("xmlreader");

var { APPID, ShopNum, SECRET_KEY, ShopKey } = require("../models/config")

const RequestUrl = "https://www.adkang.cn/admin/notifypay"

router.prefix("/admin")



router.post('/notifypay', async ctx => {
    let  xml = cxt.request.body 
    let str = xmlreader.read(body.toString("utf-8"))
    console.log(str);
    
})

router.post('/wxpay', async ctx => {
    //首先拿到前端传过来的参数
 
  
    let { orderCode, money, openid } = ctx.request.body 
  
    //首先生成签名sign
    // appid
    let mch_id = ShopNum;
    let nonce_str = wxpay.createNonceStr();
    let timestamp = wxpay.createTimeStamp();
    let body = '测试微信支付';
    let out_trade_no = orderCode;
    let total_fee = wxpay.getmoney(money);
    let spbill_create_ip = req.connection.remoteAddress; // 支持IPV4和IPV6两种格式的IP地址。调用微信支付API的机器IP
    let notify_url = RequestUrl;
    let trade_type = 'JSAPI';  // 'APP';公众号：'JSAPI'或'NATIVE'
  
    let sign = wxpay.paysignjsapi(APPID, body, mch_id, nonce_str, notify_url, openid, out_trade_no, spbill_create_ip, total_fee, trade_type, ShopKey);
  
    console.log('sign==', sign);
  
    //组装xml数据
    var formData = "<xml>";
    formData += "<appid>" + appid + "</appid>";  //appid
    formData += "<body><![CDATA[" + "测试微信支付" + "]]></body>";
    formData += "<mch_id>" + mch_id + "</mch_id>";  //商户号
    formData += "<nonce_str>" + nonce_str + "</nonce_str>"; //随机字符串，不长于32位。
    formData += "<notify_url>" + notify_url + "</notify_url>";
    formData += "<openid>" + openid + "</openid>";
    formData += "<out_trade_no>" + out_trade_no + "</out_trade_no>";
    formData += "<spbill_create_ip>" + spbill_create_ip + "</spbill_create_ip>";
    formData += "<total_fee>" + total_fee + "</total_fee>";
    formData += "<trade_type>" + trade_type + "</trade_type>";
    formData += "<sign>" + sign + "</sign>";
    formData += "</xml>";
  
    console.log('formData===', formData);
  
    var url = 'https://api.mch.weixin.qq.com/pay/unifiedorder';
  
    request({ url: url, method: 'POST', body: formData }, function (err, response, body) {
      if (!err && response.statusCode == 200) {
        console.log(body);
  
        xmlreader.read(body.toString("utf-8"), function (errors, response) {
          if (null !== errors) {
            console.log(errors)
            return;
          }
          console.log('长度===', response.xml.prepay_id.text().length);
          var prepay_id = response.xml.prepay_id.text();
          console.log('解析后的prepay_id==', prepay_id);
  
  
          //将预支付订单和其他信息一起签名后返回给前端
          let package = "prepay_id=" + prepay_id;
          let signType = "MD5";
          let minisign = wxpay.paysignjsapimini(appid, nonce_str, package, signType, timestamp, mchkey);
          let paydata = JSON.stringify({ status: '200', data: { 'appId': appid, 'partnerId': mchid, 'prepayId': prepay_id, 'nonceStr': nonce_str, 'timeStamp': timestamp, 'package': 'Sign=WXPay','paySign': minisign } })
          ctx.body = {
              code: 200,
              data: paydata
          }
  
        });
      }
    });
  
  })


module.exports = router
